#include "importbyhand.h"

// 通过构造函数或方法传递数据库对象
importbyhand::importbyhand(QSqlDatabase& db, int index, QWidget *parent) : QWidget(parent), fhDatabase(db)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QStringList headers;

    if (index == 0) {
        headers = getFlyFireParticleHeaders();
    }else if(index == 1) {
        headers = getSampleInfoHeaders();
    }else if(index == 2) {
        headers = getExperimentInfoHeaders();
    }else {
        QMessageBox::warning(this, "警告", "异常index，请检查！");
        return;
    }


    for (const QString &header : headers) {
        QHBoxLayout *rowLayout = new QHBoxLayout();

        QLabel *label = new QLabel(header, this);
        QLineEdit *lineEdit = new QLineEdit(this);
        lineEdits.append(lineEdit); // 将文本框存储到列表中

        // 添加弹簧以确保对齐
        QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);


        rowLayout->addWidget(label);
        rowLayout->addItem(spacer); // 添加弹簧
        rowLayout->addWidget(lineEdit);

        mainLayout->addLayout(rowLayout);
    }

    // 添加按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *insertButton = new QPushButton("插入", this);
    QPushButton *cancelButton = new QPushButton("取消", this);
    buttonLayout->addWidget(insertButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    if (index == 0) {
        // 连接信号和槽
        connect(insertButton, &QPushButton::clicked, this, &importbyhand::onInsertFhklButtonClicked);
        connect(cancelButton, &QPushButton::clicked, this, &importbyhand::closed);
    }else if(index == 1) {
        // 连接信号和槽
        connect(insertButton, &QPushButton::clicked, this, &importbyhand::onInsertSampleButtonClicked);
        connect(cancelButton, &QPushButton::clicked, this, &importbyhand::closed);
    }else if(index == 2) {
        // 连接信号和槽
        connect(insertButton, &QPushButton::clicked, this, &importbyhand::onInsertExperimentButtonClicked);
        connect(cancelButton, &QPushButton::clicked, this, &importbyhand::closed);
    }else {
        QMessageBox::warning(this, "警告", "异常index，请检查！");

        return;
    }
}

importbyhand::~importbyhand() {

}

void importbyhand::onInsertFhklButtonClicked() {
    // 检查所有输入框是否有数据
    /*for (QLineEdit *lineEdit : lineEdits) {
        if (lineEdit->text().isEmpty()) {
            QMessageBox::warning(this, "输入错误", "所有字段都必须填写！");
            return;
        }
    }*/

    // 检查所有输入框是否有数据，除了最后一个
    for (int i = 0; i < lineEdits.size() - 1; ++i) {
        QLineEdit *lineEdit = lineEdits.at(i);
        if (lineEdit->text().isEmpty()) {
            QMessageBox::warning(this, "输入错误", "所有字段都必须填写！");
            return;
        }
    }

    // 创建 FlyFireParticle 结构体并填充数据
    FlyFireParticle particle;
    particle.particle_id = lineEdits[0]->text();
    particle.material_name = lineEdits[1]->text();
    particle.material_type = lineEdits[2]->text();
    particle.pre_burn_weight = lineEdits[3]->text().toDouble();
    particle.diameter_length = lineEdits[4]->text();
    particle.processing_method = lineEdits[5]->text();
    particle.post_burn_weight = lineEdits[6]->text().toDouble();
    particle.post_burn_diameter_length = lineEdits[7]->text();
    particle.particle_count = lineEdits[8]->text().toInt();
    particle.origin = lineEdits[9]->text();
    particle.image_path = lineEdits[10]->text();

    // 插入数据到数据库
    QSqlQuery query(fhDatabase);
    // 准备插入数据的SQL语句
    QString sql = R"(
                  INSERT INTO `fhkl_total` (
                  `particle_id`, `material_name`, `material_type`, `pre_burn_weight`,
                  `diameter_length`, `processing_method`, `post_burn_weight`,
                  `post_burn_diameter_length`, `particle_count`, `origin`, `image_path`,
                  `created_time`, `updated_time`
                  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
                  )";
    query.prepare(sql);
    query.addBindValue(particle.particle_id);
    query.addBindValue(particle.material_name);
    query.addBindValue(particle.material_type);
    query.addBindValue(particle.pre_burn_weight);
    query.addBindValue(particle.diameter_length);
    query.addBindValue(particle.processing_method);
    query.addBindValue(particle.post_burn_weight);
    query.addBindValue(particle.post_burn_diameter_length);
    query.addBindValue(particle.particle_count);
    query.addBindValue(particle.origin);
    query.addBindValue(particle.image_path);

    if (query.exec()) {
        if (particle.image_path.trimmed() == "") {
            QMessageBox::information(this, "成功", "数据导入成功，但图片地址缺失！");
        } else {
            QMessageBox::information(this, "成功", "全部导入成功！");
        }
        close();
    } else {
        QMessageBox::critical(this, "数据库错误", "数据插入失败：" + query.lastError().text());
    }
}

void importbyhand::onInsertSampleButtonClicked() {
    // 检查所有输入框是否有数据，除了最后一个
    for (int i = 0; i < lineEdits.size() - 1; ++i) {
        QLineEdit *lineEdit = lineEdits.at(i);
        if (lineEdit->text().isEmpty()) {
            QMessageBox::warning(this, "输入错误", "所有字段都必须填写！");
            return;
        }
    }

    // 创建 SampleInfo 结构体并填充数据
    SampleInfo sample;
    sample.sample_id = lineEdits[0]->text();
    sample.sample_name = lineEdits[1]->text();
    sample.sample_type = lineEdits[2]->text();
    sample.sample_weight = lineEdits[3]->text().toDouble();
    sample.sample_dimensions = lineEdits[4]->text();
    sample.sample_thickness = lineEdits[5]->text().toInt();
    sample.sample_water_content = lineEdits[6]->text();
    sample.sample_service_life = lineEdits[7]->text();
    sample.sample_origin = lineEdits[8]->text();
    sample.sample_image_path = lineEdits[9]->text();

    // 插入数据到数据库
    QSqlQuery query(fhDatabase);
    // 准备插入数据的SQL语句
    QString sql = R"(
                  INSERT INTO `sample_total` (
                  `sample_id`, `sample_name`, `sample_type`, `sample_weight`,
                  `sample_dimensions`, `sample_thickness`, `sample_water_content`,
                  `sample_service_life`, `sample_origin`, `sample_image_path`,
                  `created_time`, `updated_time`
                  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
                  )";
    query.prepare(sql);
    query.addBindValue(sample.sample_id);
    query.addBindValue(sample.sample_name);
    query.addBindValue(sample.sample_type);
    query.addBindValue(sample.sample_weight);
    query.addBindValue(sample.sample_dimensions);
    query.addBindValue(sample.sample_thickness);
    query.addBindValue(sample.sample_water_content);
    query.addBindValue(sample.sample_service_life);
    query.addBindValue(sample.sample_origin);
    query.addBindValue(sample.sample_image_path);

    // 执行插入操作并检查结果
    if (query.exec()) {
        if (sample.sample_image_path.trimmed().isEmpty()) {
            QMessageBox::information(this, "成功", "数据导入成功，但图片地址缺失！");
        } else {
            QMessageBox::information(this, "成功", "全部导入成功！");
        }
        close(); // 关闭窗口或清空输入框
    } else {
        QMessageBox::critical(this, "数据库错误", "数据插入失败：" + query.lastError().text());
    }
}

void importbyhand::onInsertExperimentButtonClicked() {
    // 检查所有输入框是否有数据，除了最后一个
    for (int i = 0; i < lineEdits.size() - 1; ++i) {
        QLineEdit *lineEdit = lineEdits.at(i);
        if (lineEdit->text().isEmpty()) {
            QMessageBox::warning(this, "输入错误", "所有字段都必须填写！");
            return;
        }
    }

    // 创建 ExperimentInfo 结构体并填充数据
    ExperimentInfo experiment;
    experiment.test_id = lineEdits[0]->text();
    experiment.test_time = lineEdits[1]->text();
    experiment.ignition_method = lineEdits[2]->text();
    experiment.sample_id = lineEdits[3]->text();
    experiment.particle_id = lineEdits[4]->text();
    experiment.sample_name = lineEdits[5]->text();
    experiment.fire_particle = lineEdits[6]->text();
    experiment.particle_count = lineEdits[7]->text().toInt();
    experiment.test_wind_speed = lineEdits[8]->text();
    experiment.ignition_duration = lineEdits[9]->text();
    experiment.hazard_level = lineEdits[10]->text();
    experiment.image_path = lineEdits[11]->text();

    // 插入数据到数据库
    QSqlQuery query(fhDatabase);
    // 准备插入数据的SQL语句
    QString sql = R"(
                  INSERT INTO `experiment_total` (
                  `test_id`, `test_time`, `ignition_method`, `sample_id`,
                  `particle_id`, `sample_name`, `fire_particle`, `particle_count`,
                  `test_wind_speed`, `ignition_duration`, `hazard_level`, `image_path`,
                  `created_time`, `updated_time`
                  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
                  )";
    query.prepare(sql);
    query.addBindValue(experiment.test_id);
    query.addBindValue(experiment.test_time);
    query.addBindValue(experiment.ignition_method);
    query.addBindValue(experiment.sample_id);
    query.addBindValue(experiment.particle_id);
    query.addBindValue(experiment.sample_name);
    query.addBindValue(experiment.fire_particle);
    query.addBindValue(experiment.particle_count);
    query.addBindValue(experiment.test_wind_speed);
    query.addBindValue(experiment.ignition_duration);
    query.addBindValue(experiment.hazard_level);
    query.addBindValue(experiment.image_path);

    // 执行插入操作并检查结果
    if (query.exec()) {
        if (experiment.image_path.trimmed().isEmpty()) {
            QMessageBox::information(this, "成功", "数据导入成功，但图片地址缺失！");
        } else {
            QMessageBox::information(this, "成功", "全部导入成功！");
        }
        close(); // 关闭窗口或清空输入框
    } else {
        QMessageBox::critical(this, "数据库错误", "数据插入失败：" + query.lastError().text());
    }
}

void importbyhand::closed() {
    this->close();
}
